package com.hhq.Dao.Impl;

import com.hhq.Dao.EmployeeDao;
import com.hhq.entity.Employee;
import com.hhq.entity.Job;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class EmployeeDaoImpl implements EmployeeDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public List<Employee> getAllEmployee() {
        String sql = "select * from employee";
        List<Employee> list = jdbcTemplate.query(sql,new BeanPropertyRowMapper<Employee>(Employee.class));
        return list;
    }

    @Override
    public List<Employee> findEmployeeByName(String name) {
        String sql = "select * from employee where name = ?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Employee.class),name);
    }


    @Override
    public int addEmployee(Employee employee) {
        String sql = "insert into employee" +
                "(name, card_id,sex,education,email,phone,address,createDate,jobname,deptname)" +
                " value(?,?,?,?,?,?,?,?,?,?)";
        int num = jdbcTemplate.update(sql,new Object[]{
                employee.getName(),
                employee.getCard_id(),
                employee.getSex(),
                employee.getEducation(),
                employee.getEmail(),
                employee.getPhone(),
                employee.getAddress(),
                employee.getCreateDate(),
                employee.getJobname(),
                employee.getDeptname()
        });
        return num;
    }

    @Override
    public int updateEmployee(Employee employee) {
        String sql = "update employee set education=?, email=?,phone=?, address=?, createDate=?, jobname=?, deptname=? where name=?";
        Object[] objects = new Object[]{
                employee.getEducation(),
                employee.getEmail(),
                employee.getPhone(),
                employee.getAddress(),
                employee.getCreateDate(),
                employee.getJobname(),
                employee.getDeptname(),
                employee.getName()
        };
        int num = jdbcTemplate.update(sql,objects);
        return num;
    }

    @Override
    public void deleteEmployee(String name) {
        String sql = "delete from employee where name=?";
        jdbcTemplate.update(sql,new Object[]{name});
    }

}
